package com.ruoyi.SSL.controller;

import java.util.List;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.SSL.domain.SslTrusteeshipService;
import com.ruoyi.SSL.service.ISslTrusteeshipServiceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 托管服务器Controller
 *
 * @author ruoyi
 * @date 2024-09-27
 */
@Controller
@RequestMapping("/SSL/service")
public class SslTrusteeshipServiceController extends BaseController
{
    private String prefix = "SSL/service";

    @Autowired
    private ISslTrusteeshipServiceService sslTrusteeshipServiceService;

    @RequiresPermissions("SSL:service:view")
    @GetMapping()
    public String service()
    {
        return prefix + "/service";
    }

    /**
     * 查询托管服务器列表
     */
    @RequiresPermissions("SSL:service:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SslTrusteeshipService sslTrusteeshipService)
    {
        startPage();
        List<SslTrusteeshipService> list = sslTrusteeshipServiceService.selectSslTrusteeshipServiceList(sslTrusteeshipService);
        for (SslTrusteeshipService trusteeshipService : list) {
            if(DateUtil.between(trusteeshipService.getLastCheckDate(),DateUtil.date(), DateUnit.DAY, true) < 2){
                trusteeshipService.setStatus("Y");
            }else{
                trusteeshipService.setStatus("N");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出托管服务器列表
     */
    @RequiresPermissions("SSL:service:export")
    @Log(title = "托管服务器", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SslTrusteeshipService sslTrusteeshipService)
    {
        List<SslTrusteeshipService> list = sslTrusteeshipServiceService.selectSslTrusteeshipServiceList(sslTrusteeshipService);
        ExcelUtil<SslTrusteeshipService> util = new ExcelUtil<SslTrusteeshipService>(SslTrusteeshipService.class);
        return util.exportExcel(list, "托管服务器数据");
    }

    /**
     * 新增托管服务器
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存托管服务器
     */
    @RequiresPermissions("SSL:service:add")
    @Log(title = "托管服务器", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SslTrusteeshipService sslTrusteeshipService)
    {
        return toAjax(sslTrusteeshipServiceService.insertSslTrusteeshipService(sslTrusteeshipService));
    }

    /**
     * 修改托管服务器
     */
    @RequiresPermissions("SSL:service:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SslTrusteeshipService sslTrusteeshipService = sslTrusteeshipServiceService.selectSslTrusteeshipServiceById(id);
        mmap.put("sslTrusteeshipService", sslTrusteeshipService);
        return prefix + "/edit";
    }

    /**
     * 修改保存托管服务器
     */
    @RequiresPermissions("SSL:service:edit")
    @Log(title = "托管服务器", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SslTrusteeshipService sslTrusteeshipService)
    {
        return toAjax(sslTrusteeshipServiceService.updateSslTrusteeshipService(sslTrusteeshipService));
    }

    /**
     * 删除托管服务器
     */
    @RequiresPermissions("SSL:service:remove")
    @Log(title = "托管服务器", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sslTrusteeshipServiceService.deleteSslTrusteeshipServiceByIds(ids));
    }

    @ResponseBody
    @GetMapping("/queryTree")
    public List<Ztree> queryServiceTree()
    {
        return sslTrusteeshipServiceService.selectTree(new SslTrusteeshipService());
    }

    @ResponseBody
    @GetMapping("/scriptInstallUri")
    public AjaxResult scriptInstallUri(){
        String script = sslTrusteeshipServiceService.getRunAutoScript();
        if(StringUtils.isNotEmpty(script)){
            return AjaxResult.success("复制成功！", script);
        }else{
            return AjaxResult.error("暂无安装脚本！");
        }
    }
}
